﻿--[[
   @brief    suggest predownload tips view
   @author   leozzyzheng
   @version  1.0
   @date     2015-03-26
--]]

suggest_predownload_view = {
    game_id = 0;
    margin_to_main_top = 183;
    margin_to_main_left = 2;
    margin_to_main_right = 2;
    
    BTN_CLICKED_NOTHING = 1;
    BTN_CLICKED_CLOSE = 2;
    BTN_CLICKED_OK = 3;
    
    CLOSE_REASON_NULL = 0;
    CLOSE_REASON_BTN_CLICKED = 1;
    CLOSE_REASON_WM_CLOSE = 2;
    CLOSE_REASON_AVOID_OTHERS = 3;
    
    btn_clicked = BTN_CLICKED_NOTHING;  -- 点了什么按钮1.啥都没点 2.点了x 3.点了确认开启
    close_reason = CLOSE_REASON_NULL;   -- 关闭原因1.点了按钮 2.系统消息关闭 3.避让其他窗口 
}

function suggest_predownload_view:open(game_id, number)
    base_utility.log("[suggest_predownload_view]open");
    local number_str = base_utility.convert_number_to_string(number);
    if number_str == nil then
        base_utility.log("[suggest_predownload_view]convert_number_to_string failed");
        return;
    end;
    -- 检查是否已经弹框，如果有了就不弹
    local pDlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUGGEST_PREDOWNLOAD_DIG);
    if pDlg == nil then
        nDlg = layout_mgr.restore_layout("suggest_predownload_tips.tpf");
         if nDlg == Tenio.INVALID_CONTROLID then
            base_utility.log("[suggest_predownload_view]restore_layout suggest_predownload_tips.tpf failed.", true); 
            return;
        end;
        pDlg = ui_utility.get_ctrl_ptr_by_id(nDlg);
        if pDlg == nil then
            base_utility.log("[suggest_predownload_view]can not query suggest_predownload_tips dlg.", true); 
            return;
        end;
        
        self.game_id = game_id;
        local s_predownload_b = LoadStr("PREDOWNLOAD_BEGIN");
        local s_predownload_e = LoadStr("PREDOWNLOAD_END");
        local caption_str = s_predownload_b .. number_str .. '</f>'..s_predownload_e;
        ui_utility.setproperty_by_cid(CID_SUGGEST_PREDOWNLOAD_DETAIL_LABEL, "Caption", caption_str);
        pDlg:Show(TRUE);
    end;
    
    pDlg:Restore();
    pDlg:MoveToCenter();
    -- 原来的逻辑大界面不管位置，wegame改版后没有mini界面，暂时屏蔽这里的逻辑
    -- self:change_window_position();
    pDlg:BringToTop();
end;

-- 窗口出现的时候跑到主窗口旁边
function suggest_predownload_view:change_window_position()
    local tpf_ui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    local sys_param = ierd_tgp.tpf_ui.SysParams:new();
    tpf_ui:get_sys_params(sys_param);
    local work_area_width = sys_param.work_area_width;
    sys_param:delete();

--    -- 依赖主界面，所以这里直接使用mainframe_view应该没问题
--    if mainframe_model ~= nil and 
--       mainframe_model.cur_mainframe_mode == mainframe_model.mainframe_mode.MAX_MODE then
--        -- 大界面直接居中不管
--        return;
--    end;

--    local pMainDlg = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
--    if pMainDlg == nil or pMainDlg:IsVisible() == FALSE then
--        return;
--    end;

--    local main_rect = pMainDlg:GetRectEx(FALSE,TRUE);
--    local main_width = main_rect.right - main_rect.left;
--    local pDlg = ui_utility.get_ctrl_ptr_by_cid(CID_SUGGEST_PREDOWNLOAD_DIG);
--    local dialog_rect = pDlg:GetRectEx(FALSE,TRUE);
--    local dialog_width = dialog_rect.right - dialog_rect.left;
--    local dialog_height = dialog_rect.bottom - dialog_rect.top;

--    if main_rect.right < (work_area_width - dialog_width) then
--        --右边空间足够，直接显示在右边
--        dialog_rect.left = main_rect.right + self.margin_to_main_left;
--    else
--        --显示在左边
--        dialog_rect.left = main_rect.left - self.margin_to_main_right - dialog_width;
--    end;

--    dialog_rect.top = main_rect.top + self.margin_to_main_top;
--    dialog_rect.bottom = dialog_rect.top + dialog_height;
--    dialog_rect.right = dialog_rect.left + dialog_width;
--    pDlg:MoveEx(dialog_rect,TRUE);
--    base_utility.log("[suggest_predownload_view]change_window_position");
--    base_utility.log("[suggest_predownload_view]top:" .. dialog_rect.top);
--    base_utility.log("[suggest_predownload_view]bottom:" .. dialog_rect.bottom);
--    base_utility.log("[suggest_predownload_view]right:" .. dialog_rect.right);
--    base_utility.log("[suggest_predownload_view]left:" .. dialog_rect.left);
end;

function suggest_predownload_view.close(uilib, ctr_id)
    local nDlg = ui_utility.get_ctrl_id_by_cid(CID_SUGGEST_PREDOWNLOAD_DIG);
    if nDlg ~= Tenio.INVALID_CONTROLID then
        layout_mgr.delay_remove_ctrl(nDlg);
        suggest_predownload_view.game_id = 0;
        suggest_predownload_control:qos();
        suggest_predownload_view.btn_clicked = suggest_predownload_view.BTN_CLICKED_NOTHING;
        suggest_predownload_view.close_reason = suggest_predownload_view.CLOSE_REASON_NULL;
    end;
end;

function suggest_predownload_view.close_clicked(uilib, ctr_id)
    suggest_predownload_view.close_reason = suggest_predownload_view.CLOSE_REASON_BTN_CLICKED;
    suggest_predownload_view.btn_clicked = suggest_predownload_view.BTN_CLICKED_CLOSE;
    suggest_predownload_view.close(uilib, ctr_id);
end;

function suggest_predownload_view.ok(uilib, ctr_id)
    -- 同意开启预下载
    local game_lib =
        base_utility.get_component_adapter("IGame_library",
            "ierd_tgp.game_library.Get_game_library_adapter();");
    if game_lib ~= nil then
        game_lib:set_auto_update_game(suggest_predownload_view.game_id, 1);
    end;
    suggest_predownload_view.close_reason = suggest_predownload_view.CLOSE_REASON_BTN_CLICKED;
    suggest_predownload_view.btn_clicked = suggest_predownload_view.BTN_CLICKED_OK;
    suggest_predownload_view.close(uilib, ctr_id);
end;

function suggest_predownload_view.on_close(uilib, ctr_id)
    suggest_predownload_view.close_reason = suggest_predownload_view.CLOSE_REASON_WM_CLOSE;
    suggest_predownload_view.close(uilib, ctr_id);
    return true;
end;

